package util;

import java.io.*;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import pojos.Expenses;

public class ExpensesExcelFile {

    public String generateExcelFile(String fileName ,List<Expenses> checkedExpenses) {
        try {
            File generatedFile=new File(fileName);
            HSSFWorkbook hwb = new HSSFWorkbook();
            HSSFSheet sheet = hwb.createSheet("New Sheet");
            
            HSSFRow rowhead = sheet.createRow((short) 0);
            rowhead.createCell((short) 1).setCellValue("Expense_Type");
            rowhead.createCell((short) 2).setCellValue("Expense_Date");
            rowhead.createCell((short) 3).setCellValue("Task");
            rowhead.createCell((short) 4).setCellValue("Reimbursable");
            rowhead.createCell((short) 5).setCellValue("Units");
            rowhead.createCell((short) 6).setCellValue("Currency");
            rowhead.createCell((short) 7).setCellValue("Employee");
            rowhead.createCell((short) 8).setCellValue("Project");
            rowhead.createCell((short) 9).setCellValue("Created_At");
            rowhead.createCell((short) 10).setCellValue("Created_By");
            rowhead.createCell((short) 11).setCellValue("Status");
            
            for (int i= 0 ; i<checkedExpenses.size() ; i++) {
                HSSFRow row = sheet.createRow((short) i+1);
                row.createCell((short) 0).setCellValue(checkedExpenses.get(i).getExpId());
                row.createCell((short) 1).setCellValue(checkedExpenses.get(i).getExpenseType());
                row.createCell((short) 2).setCellValue(checkedExpenses.get(i).getExpenseDate());
                row.createCell((short) 3).setCellValue(checkedExpenses.get(i).getTask());
                row.createCell((short) 4).setCellValue(checkedExpenses.get(i).getReimbursable());
                row.createCell((short) 5).setCellValue(checkedExpenses.get(i).getUnits());
                row.createCell((short) 6).setCellValue(checkedExpenses.get(i).getCurrency().getCurrencyCode());
                row.createCell((short) 7).setCellValue(checkedExpenses.get(i).getEmployee().getBusinessEmail());
                row.createCell((short) 8).setCellValue(checkedExpenses.get(i).getProject().getProjectName());
                row.createCell((short) 9).setCellValue(checkedExpenses.get(i).getCreatedAt());
                row.createCell((short) 10).setCellValue(checkedExpenses.get(i).getCreatedBy().getBusinessEmail());
                row.createCell((short) 11).setCellValue(checkedExpenses.get(i).getStatus());
            }
            FileOutputStream fileOut = new FileOutputStream(generatedFile);
            hwb.write(fileOut);
            fileOut.close();
            return generatedFile.getAbsolutePath();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}